package com.cvte.boe.media;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.KeyEvent;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.cvte.adapter.android.os.SystemPropertiesAdapter;
import com.cvte.boe.media.player.VideoPlayer;
import com.cvte.boe.media.utils.LogUtil;
import com.cvte.boe.media.utils.OrientationUtil;
import com.cvte.boe.media.utils.SettingUtil;
import com.cvte.boe.media.utils.StringUtils;
import com.cvte.boe.media.views.AutoHideLayout;
import com.cvte.boe.media.views.GlobalDialog;
import com.cvte.boe.media.views.settingpage.PageVideoPlayerSettings;
import com.cvte.boe.media.views.widget.MarqueeTextView;

import java.io.File;

/**
 * Created by wuguitong on 2018/4/18.
 */
public class VideoPlayActivity extends Activity implements PageVideoPlayerSettings.OnVideoSettingListener,
        VideoPlayer.OnVideoPlayerListoner{
    private static final String PROPERTY_HWROTATION = "persist.sys.hwrotation";
    private Context mContext;
    private String mCurrentPath;
    private Uri intentUri;
    private AutoHideLayout ahTopBar;
    private AutoHideLayout ahBottomBar;
    private MarqueeTextView mTVName;
    private SeekBar mVideoSeebar;
    private TextView mTvCurTime;
    private TextView mTvTotalTime;
    private ImageView mStatusImageView;
    private SurfaceView mSurfaceView;
    private SurfaceView mSubtitleSurfaceView;
    private TextView mSubtitleTextView;
    private VideoPlayer mVideoPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        if(OrientationUtil.isLandscapeOrientation(mContext)){
            SystemPropertiesAdapter.set(PROPERTY_HWROTATION,"0");
        }else{
            SystemPropertiesAdapter.set(PROPERTY_HWROTATION,"90");
        }
        setContentView(R.layout.activity_video_player);
        Intent intent = getIntent();
        String action = intent.getAction();
        if (Intent.ACTION_VIEW.equals(action)) {
            intentUri = intent.getData();
            if(intentUri != null){
                mCurrentPath = new File(intentUri.getPath()).getParent();
            }
        }
        MediaApplication.registerMountActionListener(mountActionListener);
        ahTopBar = (AutoHideLayout)findViewById(R.id.ah_video_player_top);
        ahBottomBar = (AutoHideLayout)findViewById(R.id.ah_video_player_bottom);
        mTVName = (MarqueeTextView)findViewById(R.id.tv_video_name);
        mTVName.setMarqueeState(true);
        mTvCurTime = (TextView)findViewById(R.id.tv_video_cur_time);
        mTvTotalTime = (TextView)findViewById(R.id.tv_video_total_time);
        mVideoSeebar = (SeekBar)findViewById(R.id.sb_video_time);
        mSurfaceView = (SurfaceView)findViewById(R.id.sv_video_player_suface);
        mSubtitleSurfaceView = (SurfaceView)findViewById(R.id.sv_video_player_subtitle_surface);
        mSubtitleTextView = (TextView)findViewById(R.id.tv_subtitle_text);
        mStatusImageView = (ImageView)findViewById(R.id.iv_video_status);
        mVideoPlayer = new VideoPlayer();
        mVideoPlayer.initPlayer(mContext,intentUri.getPath(),mCurrentPath,ahTopBar,ahBottomBar,mTVName,mTvCurTime,mTvTotalTime,mVideoSeebar,mSurfaceView,mSubtitleTextView,mStatusImageView);
        mVideoPlayer.setOnVideoPlayerListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_MENU:
                PageVideoPlayerSettings pageVideoPlayerSettings = new PageVideoPlayerSettings(mContext);
                pageVideoPlayerSettings.setOnVideoPlayerListener(this);
                GlobalDialog.getInstance(this).setContentView(pageVideoPlayerSettings);
                GlobalDialog.getInstance(this).show();
                return true;
            case KeyEvent.KEYCODE_BACK:
                this.finish();
                return true;
            case KeyEvent.KEYCODE_DPAD_CENTER:
                mVideoPlayer.changeVideoPlayStatus();
                return true;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                mVideoPlayer.backwardVideo();
                return true;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                mVideoPlayer.forwardVideo();
                return true;
            default:
                break;
        }
        return false;
    }
    @Override
    protected void onStop() {
        GlobalDialog.destroyInstance();
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        MediaApplication.unRegisterMountActionListener(mountActionListener);
        if(mVideoPlayer != null){
            mVideoPlayer.deinitVideoPlayer();
        }
        SystemPropertiesAdapter.set(PROPERTY_HWROTATION,"0");
        super.onDestroy();
    }

    @Override
    public boolean onUpdateVideoLoop(SettingUtil.VLOOP loop) {
        return mVideoPlayer.setVideoLoop(loop);
    }

    @Override
    public boolean onUpdateVideoSub(SettingUtil.VSUBTITLE sub) {
        return mVideoPlayer.setVideoSub(sub);
    }

    @Override
    public boolean onUpdateVideoAudio(SettingUtil.VAUDIO audio) {
        return mVideoPlayer.setVideoAudio(audio);
    }

    @Override
    public boolean onUpdateVideoDisplay(SettingUtil.VDISPLAY display) {
        return mVideoPlayer.setVideoDisplay(display);
    }
    MediaApplication.MountActionListener mountActionListener = new MediaApplication.MountActionListener() {
        @Override
        public void onReceive(String volumePath, boolean mount) {
            if (StringUtils.formatMountPointWithSeparator(mCurrentPath).startsWith(StringUtils.formatMountPointWithSeparator(volumePath))) {
                if (!mount) {
                    VideoPlayActivity.this.finish();
                }
            }
        }
    };

    @Override
    public void OnVideoPlayerInited() {

    }

    @Override
    public void OnVideoUnsupport() {
        this.finish();
    }
}
